const app = getApp();
const baseUrl = app.globalData.baseUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    trainId:null,
    elements: [{
      name: '增肌训练',
      cover_s: baseUrl + '/file/xb.jpg'
    }, {
        name: '减脂训练',
      cover_s: baseUrl + '/file/jz.jpg'
    }, {
        name: '塑形训练',
      cover_s: baseUrl + '/file/sx.jpg'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const trainId = wx.getStorageSync("trainId");
    this.setData({
      trainId
    });
  },
  toDetail: function (e) {
    const that = this;
    const index = e.currentTarget.dataset.index;
    switch (index) {
      case 0:
        that.toMuscleBuild();
        break;
      case 1:
        that.toFatLoss();
        break;
      case 2:
        that.toBodyRebuild();
        break;
    }
  },
  toMuscleBuild:function(){
    wx.setStorageSync('trainId', this.data.trainId);
    wx.navigateTo({
      // url: '/pages/addCourse/addCourse'
      url: '/pages/positionMovement/positionMovement'
    });
  },
  toFatLoss:function(){
    const courses = [];
    courses.push({name:'减脂训练'});
    const trainId = this.data.trainId;
    const url = app.globalData.url;
    wx.request({
      url: url + '/train/saveCourseTrains',
      data: {
        trainId,
        courses
      },
      method: 'POST',
      success: function (response) {
        if (response.data.success) {
          wx.navigateBack({
            data: 1
          });
          wx.showToast({
            title: '添加成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '添加课程失败:' + response.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  toBodyRebuild:function(){
    wx.showToast({
      title: '正在开发...',
      icon:'none'
    })
  }
})